from datetime import datetime

from exts import db
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField, DecimalField, DateField
from wtforms.validators import DataRequired


class Usermodel(db.Model):
    __tablename__ = "user"
    id = db.Column(db.String(100), primary_key=True, nullable=False)
    username = db.Column(db.String(100), nullable=False)
    password = db.Column(db.String(100), nullable=False)
    department = db.Column(db.String(100), nullable=False)
    role = db.Column(db.String(100), nullable=False)
    phonenumber = db.Column(db.String(100))


class Merchantmodel(db.Model):
    __tablename__ = "merchants"
    id = db.Column(db.String(100), primary_key=True, nullable=False)
    zhenyuan_name = db.Column(db.String(100), nullable=False)
    wechat_name = db.Column(db.String(100), nullable=False)
    account_no = db.Column(db.String(100), nullable=False)
    account_name = db.Column(db.String(100), nullable=False)
    bank_name = db.Column(db.String(100))

    def __repr__(self):
        return 'Merchantmodel: %s' % self.wechat_name


class Statementmodel(db.Model):
    __tablename__ = 'statements'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    merchant_id = db.Column(db.String(100), db.ForeignKey('merchants.id'))
    since = db.Column(db.Date)
    till = db.Column(db.Date)
    zhenyuan_amount = db.Column(db.DECIMAL(10, 2))
    wechat_amount = db.Column(db.DECIMAL(10, 2))
    service_fee = db.Column(db.DECIMAL(10, 2))
    statement_amount = db.Column(db.DECIMAL(10, 2))
    status = db.Column(db.String(100))
    submitter = db.Column(db.String(45))
    supervisor = db.Column(db.String(45))
    examiner = db.Column(db.String(45))
    assentor = db.Column(db.String(45))
    approver = db.Column(db.String(45))
    # 关系引用 #
    merchants = db.relationship('Merchantmodel', backref='this_merchant')


class Addstatementformmodel(FlaskForm):
    input_time = DateField('录入时间', default=datetime.today(), format='%d%m%Y%H%M', validators=[DataRequired()])
    merchant_id = StringField('商户号', validators=[DataRequired()])
    since = DateField('开始时间', validators=[DataRequired()])
    till = DateField('结束时间', validators=[DataRequired()])
    zhenyuan_amount = DecimalField('一卡通金额', validators=[DataRequired()])
    wechat_amount = DecimalField('微校金额', validators=[DataRequired()])
    service_fee = DecimalField('系统管理与维护费', validators=[DataRequired()])
    statement_amount = DecimalField('结付金额', validators=[DataRequired()])
    submit = SubmitField('提交')
